#include <memory>
#include <thread>
#include <vector>
#include <atomic>
#include <mutex>
#include <unistd.h>
#include <iostream>

#include <iostream>

using namespace std;

class PTR{

public:
    PTR()
    {
        std::cout << "PTR "  << std::endl;
    }
    ~PTR()
    {
        std::cout << "~PTR "  << std::endl;
    }
private:

};

std::shared_ptr<PTR> ptr = make_shared<PTR>();

mutex mut;

void ThreadFunc(int num) {
    //加锁解决当前同步写的问题
    mut.lock();

    //内部有风险，取数据无所谓，如果是写数据
    shared_ptr<PTR> innerPtr = ptr;
    std::cout  << ptr.use_count() << std::endl;

    mut.unlock();
}

int main() {

    std::thread t1(ThreadFunc, 1);
    std::thread t2(ThreadFunc, 2);
    std::thread t3(ThreadFunc, 3);
    std::thread t4(ThreadFunc, 4);
    std::thread t5(ThreadFunc, 5);
    t1.join();

    system("pause");

    return 0;
}